package com.settlement.system.service.impl;

import cn.hutool.core.lang.Assert;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.settlement.system.common.enums.WebHookEnum;
import com.settlement.system.common.model.Option;
import com.settlement.system.mapper.WebHookMapper;
import com.settlement.system.model.entity.WebHook;
import com.settlement.system.model.form.WebHookForm;
import com.settlement.system.model.query.WebHookPageQuery;
import com.settlement.system.service.SysDictService;
import com.settlement.system.service.WebHookService;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * WEB_HOOK表 服务实现类
 * </p>
 *
 * @author huangrensen
 * @since 2023-10-23
 */
@Service
@RequiredArgsConstructor
public class WebHookServiceImpl extends ServiceImpl<WebHookMapper, WebHook> implements WebHookService {

    private final SysDictService dictService;

    @Override
    public boolean createOrUpdate(WebHookForm webHookForm) {
        WebHook webHook = new WebHook();
        BeanUtils.copyProperties(webHookForm, webHook);
        if (webHook.getId() == null) {
            // 检查是否存在相同的配置
            List<WebHook> list = this.list(new LambdaQueryWrapper<WebHook>().eq(WebHook::getHookType, webHookForm.getHookType()).eq(WebHook::getWebHookUrl, webHookForm.getWebHookUrl()));
            Assert.isTrue(list.size() == 0, "存在相同的推送配置，请勿重复添加");
            this.baseMapper.insert(webHook);
        } else {
            // 检查是否存在相同的配置
            List<WebHook> list = this.list(new LambdaQueryWrapper<WebHook>().eq(WebHook::getHookType, webHookForm.getHookType()).eq(WebHook::getWebHookUrl, webHookForm.getWebHookUrl()).ne(WebHook::getId, webHookForm.getId()));
            Assert.isTrue(list.size() == 0, "存在相同的推送配置，请勿重复添加");
            this.baseMapper.updateById(webHook);
        }
        return true;
    }

    @Override
    public Page<WebHook> getPage(WebHookPageQuery queryParams) {
        // 查询参数
        int pageNum = queryParams.getPageNum();
        int pageSize = queryParams.getPageSize();
        // 查询数据
        Page<WebHook> page = new Page<>(pageNum, pageSize);
        page = this.baseMapper.getPage(page, queryParams);
        List<Option> options = dictService.listDictOptions("WebHook");
        for (WebHook record : page.getRecords()) {
            options.stream().filter(option -> option.getValue().equals(record.getHookType())).findFirst().ifPresent(option -> {
                record.setHookType(option.getLabel());
            });
        }
        return page;
    }

    @Override
    @SuppressWarnings("unchecked")
    public boolean deleteWebHooks(String idsStr) {
        Assert.isTrue(StrUtil.isNotBlank(idsStr), "删除的钩子数据为空");
        // 逻辑删除
        List<Long> ids = Arrays.stream(idsStr.split(",")).map(Long::parseLong).collect(Collectors.toList());
        return this.removeByIds(ids);
    }

    @Override
    public void enable(String idsStr) {
        Assert.isTrue(StrUtil.isNotBlank(idsStr), "操作的钩子数据为空");
        // 逻辑删除
        List<Long> ids = Arrays.stream(idsStr.split(",")).map(Long::parseLong).toList();
        List<WebHook> webHookList = this.list(new LambdaQueryWrapper<WebHook>().in(WebHook::getId, ids));
        for (WebHook webHook : webHookList) {
            webHook.setStatus(WebHookEnum.ENABLE.getValue());
        }
        this.saveOrUpdateBatch(webHookList);
    }

    @Override
    public void disable(String idsStr) {
        Assert.isTrue(StrUtil.isNotBlank(idsStr), "操作的钩子数据为空");
        // 逻辑删除
        List<Long> ids = Arrays.stream(idsStr.split(",")).map(Long::parseLong).toList();
        List<WebHook> webHookList = this.list(new LambdaQueryWrapper<WebHook>().in(WebHook::getId, ids));
        for (WebHook webHook : webHookList) {
            webHook.setStatus(WebHookEnum.DISABLE.getValue());
        }
        this.saveOrUpdateBatch(webHookList);
    }

    @Override
    public WebHook getWebHookFormData(Long id) {
        return getById(id);
    }
}
